from examination.scoring.interface.scoring_utils import ScoringUtils
import random

class CodeScoring(ScoringUtils):
    def __init__(self):
        super().__init__()

    # 计算文本相似度，相似度按0-1范围计算
    def calculate_similarity(self, text1, text2):
        """
        计算文本相似度，相似度按0-1范围计算
        """
        # 计算文本长度
        len1 = len(text1)
        len2 = len(text2)
        if len1 == 0 or len2 == 0:
            return 0
        
        # 模拟计算逻辑，实际应用中需要根据具体需求进行修改
        return random.randint(0,100) / 100


    def get_score(self, correct_answer, user_answer, score):
        """
        计算分数
        """
        percent = self.calculate_similarity(correct_answer, user_answer)
        return percent * score

if __name__ == "__main__":
    code_scoring = CodeScoring()
    print(code_scoring.get_score("def reverse_string(s):\n    return s[::-1]", "def reverse_string(s):\n    return s[::-1]", 100))